﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.OtbData.Modify
{
    public class PresenterModifyOtbData
    {
        IModifyBasicOtbDataView viewModifyBasic;
        IModifyPrincipalView view;
        Model.OtbData otbData = new Model.OtbData() { Person = new Model.Person() };

        public PresenterModifyOtbData(IModifyPrincipalView view)
        {
            this.view = view;
        }

        public void GoStepTwo()
        {
            view.ModifyBasic.ChargeDataToBasic();
            otbData = view.ModifyBasic.OtbDataToModify;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void ModifyOtbData()
        {
            view.ModifyBasic.ChargeDataToBasic();
            otbData = view.ModifyBasic.OtbDataToModify;
            if (!PassFirstStep())
            {
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            else if (!PassSecondStep())
            {
                view.CallSecondPage();
                ProcedureAfterCall();
            }
            else
            {
                view.RefreshOtbDataMainView(otbData.Background, otbData.Name);
                ManagerOtbData.ModifyOtbData(otbData);
                view.SystemMessage("Los datos Fueron Modificados Exitosamente.!!!!");
                view.ReturnSearchOtbData();
            }
        }

        private bool PassFirstStep()
        {
            try
            {
                Model.OtbData mp = new Model.OtbData();
                mp.Name = view.ModifyBasic.OtbDataToModify.Name;
                mp.Managment = view.ModifyBasic.OtbDataToModify.Managment;
                mp.ExpirationDay = view.ModifyBasic.OtbDataToModify.ExpirationDay;
                mp.Background = view.ModifyBasic.OtbDataToModify.Background;
                ManagerOtbData.VerifyOtbDataIsCorrect(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }
        
        private bool PassSecondStep()
        {

            try
            {
                Model.OtbData mp = new Model.OtbData() { Person = new Model.Person() };
                mp.Person.PersonId = view.ModifyBasic.OtbDataToModify.Person.PersonId;
                mp.Person.Names = view.ModifyBasic.OtbDataToModify.Person.Names;
                mp.Person.LastNames = view.ModifyBasic.OtbDataToModify.Person.LastNames;
                mp.Person.Phone = view.ModifyBasic.OtbDataToModify.Person.Phone;

                ManagerOtbData.VerifyDataPerson(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                return false;
            }
        }

        public void GoBackPage()
        {
            viewModifyBasic.GoBackPage();
        }

        public void GoNextPage()
        {
            viewModifyBasic.GoNextPage();
        }
        public void GoStepOne()
        {
            view.ModifyBasic.ChargeDataToBasic();
            otbData = view.ModifyBasic.OtbDataToModify;
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        private void ProcedureAfterCall()
        {

            if (viewModifyBasic != null)
                otbData = viewModifyBasic.OtbDataToModify;
            viewModifyBasic = view.ModifyBasic;
            viewModifyBasic.OtbDataToModify = otbData;
            viewModifyBasic.UpdateViewData();
        }

        public void FirstTime()
        {
            try
            {
                otbData = ManagerOtbData.GetCurrentOtbData();
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
                view.GoRegisterOtbData();
            }

        }

        public void NotifyErrorMessage(string MessageValue)
        {
            view.ErrorMessage(MessageValue);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            view.SystemMessage(MessageValue);
        }
    }
}
